package com.bonc.modules.rpt.controller;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonc.common.utils.Result;
import com.bonc.modules.rpt.enums.RptEnum;
import com.bonc.modules.rpt.param.FixParam;
import com.bonc.modules.rpt.service.FixService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author jinlong
 * @date 2021/6/28
 */
@Controller
@RequestMapping(value = "/rpt/fix")
public class FixController {

    @GetMapping("/{rptName}/maxDate")
    @ResponseBody
    public Result maxDate(FixParam fixParam, @PathVariable String rptName) {

        String maxDate = SpringUtil.getBean(rptName, FixService.class).getMaxDate();

        return Result.ok().put("maxDate", maxDate);
    }

    @GetMapping("/{rptName}/page")
    @ResponseBody
    public Result page(FixParam fixParam, @PathVariable String rptName) {
        Page page = SpringUtil.getBean(rptName, FixService.class).queryPage(fixParam);
        return Result.ok().put("page", page);
    }


    @GetMapping("/{rptName}/data")
    @ResponseBody
    public Result data(FixParam fixParam, @PathVariable String rptName) {
        List data = SpringUtil.getBean(rptName, FixService.class).queryData(fixParam);
        return Result.ok().put("list", data);
    }

    /**
     * 文件下载并且失败的时候返回json
     */
    @GetMapping("/{rptName}/excel")
    public void downloadFailedUsingJson(HttpServletResponse response, FixParam fixParam, @PathVariable String rptName) throws IOException {
        List data = SpringUtil.getBean(rptName, FixService.class).queryData(fixParam);
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode(RptEnum.valueOf(rptName).getRptName(), "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), RptEnum.valueOf(rptName).getClassType()).autoCloseStream(Boolean.TRUE).sheet("sheet1")
                    .doWrite(data);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(JSONUtil.toJsonStr(Result.error("下载文件失败,请联系管理员")));
        }
    }

    /**
     * 上传文件
     *
     * @author stylefeng
     * @date 2020/6/7 22:15
     */
    @PostMapping("/{rptName}/upload")
    @ResponseBody
    public Result upload(@RequestPart("file") MultipartFile file, FixParam fixParam, @PathVariable String rptName) {
        Long fileId = SpringUtil.getBean(rptName, FixService.class).uploadFile(file, fixParam);
        return Result.ok().put("data", fileId);
    }

    /**
     * 下载文件
     *
     * @author stylefeng, xuyuxiang
     * @date 2020/6/9 21:53
     */
    @GetMapping("/{rptName}/download")
    @ResponseBody
    public void download(FixParam fixParam, HttpServletResponse response, @PathVariable String rptName) {
        SpringUtil.getBean(rptName, FixService.class).downloadFile(fixParam, response);
    }


    /**
     * 删除文件
     *
     * @author stylefeng
     * @date 2020/6/7 22:15
     */
    @PostMapping("/{rptName}/delete")
    @ResponseBody
    public Result delete(@RequestBody FixParam fixParam, @PathVariable String rptName) {
        SpringUtil.getBean(rptName, FixService.class).deleteFile(fixParam);
        return Result.ok();
    }
}
